package com.itheima.controller;

import com.itheima.anno.Log;
import com.itheima.pojo.Emp;
import com.itheima.pojo.Result;
import com.itheima.service.EmpService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * 员工管理Controller
 */
@Slf4j
@RequestMapping("/emps")
@RestController
public class EmpController {

    @Autowired
    private EmpService empService;

    /**
     * 分页展示员工列表
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping
    public Result getEmps(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer pageSize) {
        log.info("获取员工列表");
        return Result.success(empService.getEmps(page,pageSize));
    }


    /**
     * 批量删除员工
     * @param ids
     * @return
     */
    @Log
    @DeleteMapping("/{ids}")
    public Result deleteEmps(@PathVariable("ids") List<Integer> ids) {
        log.info("删除员工列表");
        empService.deleteEmps(ids);
        return Result.success();
    }

    @Log
    @PostMapping
    public Result addEmp(@RequestBody Emp emp) {
        log.info("新增员工");
        empService.addEmp(emp);
        return Result.success();
    }

    @GetMapping("/{id}")
    public Result searchEmp(@PathVariable("id") Integer id) {
        log.info("根据ID查询员工, id={}", id);
        return Result.success(empService.searchEmp(id));
    }

    @Log
    @PutMapping
    public Result updateEmp(@RequestBody Emp emp) {
        log.info("更新员工");
        empService.updateEmp(emp);
        return Result.success();
    }

}
